package org.answer.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.answer.dao.PermissionDao;
import org.answer.entity.PageResult;
import org.answer.entity.QueryPageBean;
import org.answer.pojo.CheckItem;
import org.answer.pojo.Permission;
import org.answer.pojo.Role;
import org.answer.service.PermissionService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import java.util.Set;
import java.util.function.Predicate;
@Service(interfaceClass = PermissionService.class)
@Transactional
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionDao permissionDao;

    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //  完成分页查询，基于mybatis框架提供的分页助手插件完成
        PageHelper.startPage(currentPage, pageSize);

        Page<Permission> page = permissionDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Permission> rows = page.getResult();
        return new PageResult(total, rows);
    }

    @Override
    public void update(Permission permission) {
        Integer update = permissionDao.update(permission);
    }

    @Override
    public void delete(Integer id) {
        //清理中间关系表
        permissionDao.deleteAssocication(id);
        //根据对应权限id删除
        permissionDao.deleteById(id);
    }

    @Override
    public String add(Permission permission) {
        try {
            permissionDao.add(permission);
            return null;
        } catch (DuplicateKeyException e) {
            //如果插入的数据已存在则抛出DuplicateKeyException异常
            return "DuplicateKeyException";
        }
    }
    public List<Permission> findAllPermission() {
        return permissionDao.findAll();
    }

}
